package cn.tiasen.helloworld.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;

import java.util.ArrayList;
import java.util.List;

import cn.tiasen.helloworld.vo.AppInfo;

import static android.content.pm.PackageManager.MATCH_ALL;
import static android.content.pm.PackageManager.MATCH_DEFAULT_ONLY;

public class Utils {

    public static List<AppInfo> getAllApps(Context context){
        List<AppInfo> appinfos = new ArrayList<>();
        // 得到应用packagemanager
        PackageManager packageManager = context.getPackageManager();

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);

        List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(intent, MATCH_DEFAULT_ONLY);
        for(ResolveInfo info : resolveInfos){
            String packageName = info.activityInfo.packageName;
            Drawable icon = info.loadIcon(packageManager);
            String appname = info.loadLabel(packageManager).toString();
            AppInfo appInfo = new AppInfo(appname, packageName, icon);
            appinfos.add(appInfo);


        }
        return appinfos;
    }
}
